<?php

namespace front\modules\app\controllers;

use common\helpers\ReturnHelper;
use front\controllers\RestBaseController;
use goods\services\ProductService;
use Yii;
use yii\helpers\Json;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use goods\services\CollectionService;

/**
 * CollectionController implements the CRUD actions for CollectionModel model.
 */
class CollectionController extends RestBaseController
{

    /**
     *  取消单个商品收藏
     * @return \yii\web\Response
     */
    public function actionRemove()
    {
        $param = yii::$app->request->post();
        if(!isset($param['id'])){
            return ReturnHelper::error('该收藏不存在');
        }
        $info = CollectionService::remove($param['id']);
        if($info['code']==1){
            return ReturnHelper::error('删除失败');
        }
        return ReturnHelper::success('success');
    }

    /**
     *  清空回收站
     * @return \yii\web\Response
     */
    public function actionClean()
    {
        $param = yii::$app->request->post();
        if(!isset($param['C_Id'])){
            return ReturnHelper::error('用户ID不能为空');
        }
        CollectionService::clean($param['C_Id']);
        return ReturnHelper::success('success');
    }

    /**
     * 添加商品收藏
     *   $params=[
     *       'C_Id'=>20,
     *       'P_Id'=>20,
     *       'p_name'=>'ewqe',
     *       'pic'=>'http://www.baidu.com',
     *       'attributes'=>[
     *           'color' => '红色',
     *           'size' => '大',
     *           'price' => '255'
     *       ],
     *   ];
     */
    public function actionAdd(){
        $params = yii::$app->request->post();
        $info = CollectionService::addCollection($params);
        if($info['code']==1){
           return ReturnHelper::error('参数错误');
        }
        return ReturnHelper::success('success');
    }

    /**
     * Lists all CollectionModel models.
     * @return mixed
     */
    public function actionList()
    {
        $data = \yii::$app->request->post();
        $data['getByWeb'] = 1;
        if(!isset($data['user_id'])){
            return ReturnHelper::error("用户ID未传值");
        }
        $data['C_Id'] = \yii::$app->request->post('user_id');
        $info = CollectionService::getCollection($data);
        $info = ArrayHelper::toArray($info['result']);
        if(!empty($info)) {
            foreach ($info as $key => $item) {
                $price = ProductService::getPrice($item['P_Id']);
                $info[$key]['price'] = $this->price_format($price);
                if(empty($info[$key]['attributes'])){
                    $info[$key]['attributes'] = '';
                }
            }
        }else{
            $info = [];
        }
        return ReturnHelper::success('success', $info);
    }


}
